package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WorkArea;
import com.ruoyi.system.mapper.WorkAreaMapper;
import com.ruoyi.system.service.IWorkAreaService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-14
 */
@Service
public class WorkAreaServiceImpl  extends ServiceImpl<WorkAreaMapper, WorkArea> implements IWorkAreaService
{
    @Autowired
    private WorkAreaMapper workAreaMapper;

    private static List<WorkArea> all;
    /**
     * 查询地区
     *
     * @param id 地区主键
     * @return 地区
     */
    @Override
    public WorkArea selectWorkAreaById(Long id)
    {
        return workAreaMapper.selectWorkAreaById(id);
    }

    /**
     * 查询地区列表
     *
     * @param workArea 地区
     * @return 地区
     */
    @Override
    public List<WorkArea> selectWorkAreaList(WorkArea workArea)
    {
        return workAreaMapper.selectWorkAreaList(workArea);
    }

    /**
     * 新增地区
     *
     * @param workArea 地区
     * @return 结果
     */
    @Override
    public int insertWorkArea(WorkArea workArea)
    {
        workArea.setCreateTime(DateUtils.getNowDate());
        return workAreaMapper.insertWorkArea(workArea);
    }

    /**
     * 修改地区
     *
     * @param workArea 地区
     * @return 结果
     */
    @Override
    public int updateWorkArea(WorkArea workArea)
    {
        return workAreaMapper.updateWorkArea(workArea);
    }

    /**
     * 批量删除地区
     *
     * @param ids 需要删除的地区主键
     * @return 结果
     */
    @Override
    public int deleteWorkAreaByIds(Long[] ids)
    {
        return workAreaMapper.deleteWorkAreaByIds(ids);
    }

    /**
     * 删除地区信息
     *
     * @param id 地区主键
     * @return 结果
     */
    @Override
    public int deleteWorkAreaById(Long id)
    {
        return workAreaMapper.deleteWorkAreaById(id);
    }
    
	@Override
	public List<WorkArea> getWorkAreaTreeList(WorkArea WorkArea) {
		QueryWrapper<WorkArea> queryWrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(WorkArea.getParentCode())) {
			queryWrapper.eq("parent_code", WorkArea.getParentCode());
		}
		if (!StringUtils.isEmpty(WorkArea.getName())) {
			queryWrapper.eq("name", WorkArea.getName());
		}
		
		List<WorkArea> listTree = Lists.newArrayList();
		WorkArea productClass = new WorkArea();
		//productClass.setClassName("产品大类");
		//productClass.setClassId(0L);
		//productClass.setParentId(0L);
		//listTree.add(productClass);
		if(all == null) {
		   all = workAreaMapper.selectList(new QueryWrapper());
		}
		List<WorkArea> nodeList = workAreaMapper.selectList(queryWrapper);
		
		List<WorkArea> list = Lists.newArrayList();
		for (WorkArea node : nodeList) {
			getChildTree(node, list, all);
		}
		listTree.addAll(nodeList);
		return listTree;
	}
	
	/**
	 * 递归查找展开虚树型结构.
	 */
	public List<WorkArea> getChildTree(WorkArea root, List<WorkArea> bomStructuresDTOList, List<WorkArea> all) {
		// 获取当前root的子级
		String parentId = root.getAreaCode();
		QueryWrapper<WorkArea> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parent_code", parentId);

		
		List<WorkArea> nodeList = getChild(parentId, all);
		// 把子级循环一遍
		if(!nodeList.isEmpty()) {
			root.setChildren(nodeList);
		}
		for (WorkArea node : nodeList) {

			bomStructuresDTOList.add(node);
			List<WorkArea> child = getChildTree(node, bomStructuresDTOList, all);

		}

		// 递归退出条件没有下一层级
		if (nodeList == null || nodeList.size() == 0) {
			return Lists.newArrayList();
		}
		return nodeList;
	}
	public List<WorkArea> getChild(String parentCode, List<WorkArea> all){
		List list = Lists.newArrayList();
		for(WorkArea a : all) {
			if(a.getParentCode().equals(parentCode)) {
				list.add(a);	
			}
		}
		return list;
	}

	public List<WorkArea> getParentList(String name) {
		QueryWrapper<WorkArea> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("area_code",name);
		List<WorkArea> list = workAreaMapper.selectList(queryWrapper);
		List<WorkArea> result = Lists.newArrayList() ;
		for(WorkArea a : list) {
			getPrioTree(a,result);
		}
		Collections.reverse(result);
		return result;
	}
	/**
	 * 递归查找展开虚树型结构.
	 */
	public List<WorkArea> getPrioTree(WorkArea root, List<WorkArea> bomStructuresDTOList) {
		// 获取当前root的子级
		String parentId = root.getParentCode();
		QueryWrapper<WorkArea> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("area_code", parentId);
		List<WorkArea> nodeList = workAreaMapper.selectList(queryWrapper);
		bomStructuresDTOList.add(root);
		for (WorkArea node : nodeList) {
			List<WorkArea> child = getPrioTree(node, bomStructuresDTOList);
		}

		// 递归退出条件没有下一层级
		if (nodeList == null || nodeList.size() == 0) {
			return Lists.newArrayList();
		}
		return nodeList;
	}
}
